import { WereWolfOutPhase } from "@/werewolf/phases/out";
import { WereWolfPhaseView } from "@/werewolf/phaseView";
import { ScriptRunner } from "@sapi-game/Runner/scriptRunner";
import { CampViewTools } from "./tools";
import { Dimensions } from "@sapi-game/constants";
import { BuildWereWolfPlayerName } from "@/werewolf/func";
import { Game } from "@sapi-game/main";
import { EffectIds } from "@sapi-game/utils/vanila-data";
import { GameMode, world } from "@minecraft/server";
import { EventSubscription } from "@sapi-game/gameEvent/eventManager";

export class OutCampView extends WereWolfPhaseView<WereWolfOutPhase> {
    sub?: EventSubscription;

    override onAttach(): void {
        if (!this.state.outPlayer) {
            this.runner.run(this.tie.bind(this));
        } else {
            this.runner.run(this.runPhaseView.bind(this));
        }
    }

    //平局
    async tie(r: ScriptRunner) {
        this.players.title(
            "§e§l投票结果",
            this.state.outPlayer == null ? "§3弃票" : "§a平局"
        );
        await r.wait(40);
        this.state.next();
    }

    override async runPhaseView(r: ScriptRunner): Promise<void> {
        this.players.title(
            "§e§l投票结果",
            `玩家${BuildWereWolfPlayerName(this.state.outPlayer!)}⭐被投出游戏`
        );
        CampViewTools.resetStructure();
        for (let i = 0; i < 20; i++) {
            await r.wait(10);
            if (i == 15) {
                this.players.title("§cWarning", "§e§l发射台正在构筑");
            }
            Dimensions.Overworld.runCommand(
                "function werewolf/animation/a" + i
            );
        }
        this.players.forEach((p) =>
            p.player.onScreenDisplay.updateSubtitle("§b§l人间大炮即将发射")
        );
        await this.Launch(r);
        await r.wait(80);
        this.state.next();
    }

    async Launch(r: ScriptRunner) {
        this.tpToLaunchPos();
        r.wait(80);
        //倒计时
        let pitch = 0.5;
        for (let i = 5; i >= 0; i--) {
            await r.wait(20);
            const color = i <= 2 ? (i == 1 ? "§c" : "§e") : "§a";
            if (i == 4) {
                //生成TNT
                Dimensions.Overworld.runCommand("function werewolf/tnt");
            }
            this.players.title(`${color}${i}`);
            this.players.playSound("note.bit", { pitch: pitch });
            pitch += 0.1;
        }
        //生成烟花
        world.structureManager.place("lrs_firerocket", Dimensions.Overworld, {
            x: -187,
            y: -64,
            z: -204,
        });
        //发射
        this.players.title("§b§l发射！");
        if (this.sub) this.unsubscribe(this.sub);
        this.state.outPlayer!.player?.addEffect(EffectIds.Levitation, 20, {
            amplifier: 100,
            showParticles: false,
        });
        await r.wait(5);
        //tp烟花
        Dimensions.Overworld.getEntities({
            location: { x: -187, y: -55, z: -203 },
            maxDistance: 20,
            type: "fireworks_rocket",
        }).forEach((e) => e.teleport({ x: -201, y: -30, z: -181 }));
        await r.wait(50);
        //改旁观
        this.state.outPlayer!.player?.setGameMode(GameMode.Spectator);
        this.players.sendMessage(
            `§a玩家${BuildWereWolfPlayerName(this.state.outPlayer!)}§e变成了光`
        );
    }

    boom() {
        for (let i = 0; i < 5; i++) {
            Dimensions.Overworld.spawnParticle(
                "minecraft:huge_explosion_emitter",
                { x: -201, y: -45, z: -181 }
            );
        }
        //tp烟花

        // this.players.playSound("random.explode");
    }

    tpToLaunchPos() {
        this.sub = this.subscribe(Game.events.interval, () => {
            this.state.outPlayer!.player?.teleport({
                x: -200.5,
                y: -44.5,
                z: -180.5,
            });
            this.state.outPlayer!.player?.addEffect(EffectIds.Resistance, 300, {
                amplifier: 200,
                showParticles: false,
            });
        });
    }
}
